﻿using BrnMall.Core.Domain.Rader;
using BrnMall.Manager.Domain.Rander;
using BrnMall.Manager.Manager.AddressBook;
using BrnMall.Manager.Manager.Radar;
using System;


namespace BrnMall.Services.WJK
{
    /// <summary>
    /// 微街推-会员操作行为记录
    /// </summary>
    public class ActionRecords
    {
        /// <summary>
        /// 名片点赞记录
        /// </summary>
        public static void AddCardUpRecord(int uid, int staffId)
        {
            var model = new ActionRecord() { ActionType = (int)ActionRecordState.AddCardUp, Addtime = DateTime.Now, DateNumber = Convert.ToInt32(DateTime.Now.ToString("yyyyMMdd")), StaffId = staffId, Uid = uid };
            SendMessage(model.ActionType, model.StaffId, model.Uid);
            ActionRecordManager.Add(model);
        }

        /// <summary>
        /// 名片标签点赞记录
        /// </summary>
        public static void AddTagUpRecord(int uid, int staffId)
        {
            var model = new ActionRecord() { ActionType = (int)ActionRecordState.AddTagUp, Addtime = DateTime.Now, DateNumber = Convert.ToInt32(DateTime.Now.ToString("yyyyMMdd")), StaffId = staffId, Uid = uid };
            SendMessage(model.ActionType, model.StaffId, model.Uid);
            ActionRecordManager.Add(model);
        }

        /// <summary>
        /// 个性签名点赞记录
        /// </summary>
        public static void AddSignUpRecord(int uid, int staffId)
        {
            var model = new ActionRecord() { ActionType = (int)ActionRecordState.AddSignUp, Addtime = DateTime.Now, DateNumber = Convert.ToInt32(DateTime.Now.ToString("yyyyMMdd")), StaffId = staffId, Uid = uid };
            SendMessage(model.ActionType, model.StaffId, model.Uid);
            ActionRecordManager.Add(model);
        }


        /// <summary>
        /// 查看名片记录
        /// </summary>
        public static void AddShowCardRecord(int uid, int staffId)
        {
            var model = new ActionRecord() { ActionType = (int)ActionRecordState.ShowCard, Addtime = DateTime.Now, DateNumber = Convert.ToInt32(DateTime.Now.ToString("yyyyMMdd")), StaffId = staffId, Uid = uid };
            SendMessage(model.ActionType, model.StaffId, model.Uid);
            ActionRecordManager.Add(model);
        }
        /// <summary>
        /// 添加操作轨迹,同时给AI销售员发送模板通知
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static bool AddActionRecord(ActionRecord model)
        {
            SendMessage(model.ActionType, model.StaffId, model.Uid);
            return ActionRecordManager.Add(model);
        }

        /// <summary>
        /// 发送操作轨迹的通知
        /// </summary>
        /// <param name="actionType"></param>
        /// <param name="staffId"></param>
        /// <param name="cliengUid"></param>
        public static void SendMessage(int actionType, int staffId, int cliengUid)
        {

            //客户
            var user = ClientManager.GetByUid(cliengUid, staffId);
            //AI销售员
            var staff = StaffManager.Get(staffId);
            //企业微信的授权企业
            var perCode = WorkWeChats.GetAccessToken(staff.StoreId);

            var msg = GetActionTitle(actionType);

            msg = string.Format("{0}{1}", msg, user.MemoName);

            //发送消息
            var errMsg = BrnMall.WeiXin.Sdk.Work.WorkWeiXinSDK.SendTextMessage(msg, staff.wxUserId, perCode.agentid, perCode.access_token);

            var logModel = new Manager.Domain.Log.Logs()
            {
                Details = string.Format("actionType={0},staffId={1},cliengUid={2},errMsg={3}", actionType, staffId, cliengUid, errMsg),
                Uid = cliengUid,
                StoreId = staffId,
                DataType = 2,
                CreateTime = DateTime.Now
            };
            Manager.Manager.Log.LogManager.Add(logModel);
        }

        public static string GetActionTitle(int actionType)
        {
            var msg = string.Empty;
            switch (actionType)
            {
                case 0: msg = "查看了您的商品"; break;
                case 1: msg = "查看了您的官网"; break;
                case 2: msg = "转发了您的名片"; break;
                case 3: msg = "复制您的微信"; break;
                case 4: msg = "查看了您的名片"; break;
                case 5: msg = "查看了您的商圈"; break;
                case 6: msg = "咨询了您的产品"; break;
                case 7: msg = "保存了您的电话"; break;
                case 8: msg = "觉得您的名片靠谱"; break;
                case 9: msg = "拨打了您的电话"; break;
                case 10: msg = "对您的名片标签点赞"; break;
                case 11: msg = "对您的个性签名点赞"; break;
                case 12: msg = "复制了您的邮箱"; break;
                case 13: msg = "转发了您的动态"; break;
                case 16: msg = "拨打了您的手机"; break;
                case 19: msg = "生成了您的名片海报"; break;
            }
            return msg;
        }
    }
}